package com.netease.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.netease.web.pojo.Content;
import com.netease.web.pojo.Person;
import com.netease.web.pojo.Trx;
import com.netease.web.pojo.model.User;
import com.netease.web.pojo.model.Buy;
import com.netease.web.pojo.model.Product;
import com.netease.web.service.impl.contServiceImpl;
import com.netease.web.service.impl.personServiceImpl;
import com.netease.web.service.impl.trxServiceImpl;

public class EntryController {
	
	trxServiceImpl trxService = new trxServiceImpl();
	contServiceImpl contentService = new contServiceImpl();
	personServiceImpl personService = new personServiceImpl();
	
	protected static boolean[] setBuySell(Trx trx, Person person) {
		boolean result[] = new boolean[2];
		for(int i = 0; i < result.length; i++){
			result[i] = false;
		}
		if(trx != null){
			result[0] = true;
			
			if (trx.getPersonId() == person.getId()){
				result[1] = true;
			} 
		}
		return result;
	}
	@RequestMapping(value = "/login")
	public String userLogin(){
		return "login";
	}
	
	@RequestMapping(value = "logout")
	public String userLogout(){
		return "login";
	}
	
	@RequestMapping(value = "/")
	public String index(ModelMap model, HttpServletRequest request){
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		Person person = personService.getPersonById(user.getId());
		ArrayList<Product> productList = new ArrayList<>();
		List<Content> contentList = contentService.getAllContent();
		for(Content content : contentList){
			Trx trx = trxService.getTrxByContentId(content.getId());
			boolean isBuy = setBuySell(trx, person)[0];
			boolean isSell = setBuySell(trx, person)[1];
			Product product = new Product(content.getId(), content.getTitle(), content.getSummary(), content.getDetail(),
					content.getImage(), content.getPrice(), trx.getPrice(), isBuy, isSell);
			productList.add(product);
		}
		model.addAttribute("productList", productList);
		return "index";
	}
	
	@RequestMapping(value = "/show")
	public String show(@RequestParam(value = "id") int contentId, ModelMap model, HttpServletRequest request){
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		Person person = personService.getPersonById(user.getId());
		Content content = contentService.getContentById(contentId);
		Trx trx = trxService.getTrxByContentId(contentId);
		boolean isBuy = setBuySell(trx, person)[0];
		boolean isSell = setBuySell(trx, person)[1];
		Product product = new Product(contentId, content.getTitle(), content.getSummary(),
				content.getDetail(), content.getImage(), content.getPrice(), trx.getPrice(), isBuy, isSell);
		model.addAttribute("product", product);
		return "show";
	}
	
	@RequestMapping("/account")
	public String account(ModelMap model, HttpServletRequest request){
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		List<Trx> trxs= trxService.getTrxesByPersonId(user.getId());
		ArrayList<Buy> buyList = new ArrayList<>();
		for(Trx trx : trxs){
			Content content = contentService.getContentById(trx.getId());
			Buy buy = new Buy(trx.getContentId(), content.getTitle(), content.getImage(), trx.getPrice(), trx.getTime());
			buyList.add(buy);
		}
		model.addAttribute("buyList", buyList);
		return "account";
	}
	
	@RequestMapping("public")
	public String showPublic(){
		return "public";
	}
	
	@RequestMapping("publicSubmit")
	public String publicSubmit(@RequestParam(value = "title") String title, @RequestParam(value = "image") String image,
			@RequestParam(value = "detail")String detail, @RequestParam("price") int price,
			@RequestParam("summary") String summary, ModelMap model){
		Content content = new Content();
		content.setDetail(detail);
		content.setImage(image);
		content.setPrice(price);
		content.setSummary(summary);
		content.setTitle(title);
		int count = 0;
		count = contentService.insertContent(content);
		if(count > 0){
			Product product = new Product(content.getId(), title, summary, detail, image, price, 0, false, false);
			model.addAttribute("product", product);
		}
		return "publicSubmit";
	}
	
	@RequestMapping("/edit")
	public String editPage(@RequestParam("id") int id, ModelMap model){
		Content content = contentService.getContentById(id);
		Product product = new Product(id, content.getTitle(), content.getSummary(),
				content.getDetail(), content.getImage(), content.getPrice(), 0, false, false);
		model.addAttribute("product", product);
		return "edit";
	}
	
	@RequestMapping("editSubmit")
	public String editSubmit(@RequestParam(value = "title") String title, @RequestParam(value = "id") int id,
			@RequestParam(value = "image") String image, @RequestParam(value = "detail") String detail,
			@RequestParam(value = "price") long price, @RequestParam(value = "summary") String summary,
			ModelMap model){
		int count = 0;
		Content content = new Content();
		content.setTitle(title);
		content.setDetail(detail);
		content.setId(id);
		content.setImage(image);
		content.setPrice(price);
		content.setSummary(summary);
		count = contentService.updataContent(content);
		if(count > 0){
			Product product = new Product(id, title, summary, detail, image, price, 0, false, false);
			model.addAttribute("product", product);
		}
		return "editSubmit";
	}
}
